<template>
  <div class="nav-bar">
    <div class="nav-center">
      <div class="nav-email">
        <i>
          <img src="http://192.168.2.27:8888/assets/img/email.png" alt="" />
        </i>
        <span>985555047@qq.com</span>
      </div>
      <div class="nav-phone">
        <i>
          <img src="http://192.168.2.27:8888/assets/img/phone.png" alt="" />
        </i>
        <span>0755 15307593077</span>
      </div>
      <div class="ul-div">
        <ul class="nav-ul" v-if="!isuser">
          <li v-for="item in loginimglist" :key="item.id">
            <router-link to="/login"
              ><img :src="item.imgurl" alt=""
            /></router-link>
          </li>
        </ul>
        <div v-else class="user-info-div">
          <span class="user-info-span">
            欢迎你！{{ userinfo.usernumber }}
          </span>
          <span @click="gouserinfo" class="user-info-center">个人中心</span>
          <span @click="gouserinfo" class="user-info-center">订单</span>
          <span @click="gouserinfo" class="user-info-center">钱包</span>
          <span @click="gouserinfo" class="user-info-center">认证</span>
          <el-popconfirm title="确认退出嘛？" @confirm="exituser">
            <template #reference>
              <el-button type="primary" size="small" class="exit-user-btn"
                >退出</el-button
              >
            </template>
          </el-popconfirm>
          <!-- <button >退出</button> -->
        </div>
        <span
          class="top-right-media"
          :class="[
            { exit: isuser == false },
            { openspan: isnavopen == false },
            { closespan: isnavopen == true },
          ]"
          @click="changetopitem()"
        ></span>
        <div
          class="user-info-div-media"
          :class="[{ exit: isuser == false }, { openlistdiv: isnavopen }]"
        >
          <span class="media-img-span">
            <img src="http://192.168.2.27:8888/assets/img/login.png" alt="" />
          </span>
          <span class="user-info-span">
            欢迎你！{{ userinfo.usernumber }}
          </span>
          <span @click="gouserinfo" class="user-info-center">个人中心</span>
          <span @click="gouserinfo" class="user-info-center">我的订单</span>
          <span @click="gouserinfo" class="user-info-center">我的钱包</span>
          <span @click="gouserinfo" class="user-info-center">我的认证</span>
          <el-popconfirm title="确认退出嘛？" @confirm="exituser">
            <template #reference>
              <el-button type="primary" size="small" class="exit-user-btn"
                >退出</el-button
              >
            </template>
          </el-popconfirm>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getuserinfo } from "network/request.js";

export default {
  name: "NavBar",
  components: {},
  data() {
    return {
      loginimglist: [
        { id: "a1", imgurl: require("assets/img/fb.png") },
        { id: "a2", imgurl: require("assets/img/ins.png") },
        { id: "a3", imgurl: require("assets/img/tt.png") },
        { id: "a4", imgurl: require("assets/img/wc.png") },
      ],
      usertoken: "",
      isuser: false,
      userinfo: {},
      isnavopen: true,
    };
  },
  created() {
    if (localStorage.getItem("token") == null) {
      console.log("no user");
      this.isuser = false;
    } else {
      this.usertoken = localStorage.getItem("token");
      this.isuser = true;
      getuserinfo(this.usertoken).then(({ data: res }) => {
        // console.log(res);
        this.userinfo = res[0];
      });
    }
  },
  methods: {
    //退出登录
    exituser() {
      this.isuser = false;
      localStorage.removeItem("token");
    },
    //跳转到个人中心页面
    gouserinfo() {
      // console.log(123);
      this.$router.push("/myinfo");
    },
    //显示隐藏顶部下拉列表
    changetopitem() {
      this.isnavopen = !this.isnavopen;
    },
  },
};
</script>

<style lang="scss" scoped>
.media-img-span {
  display: block;
  margin: 20px auto 10px auto;
  width: 50px;
  height: 50px;
  background-color: #ddd;
  position: relative;
  border-radius: 50%;
  border: 2px solid white;
}
.media-img-span img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80%;
  vertical-align: top;
}

* {
  margin: 0;
  padding: 0;
}
.nav-bar {
  box-sizing: border-box;
  width: 100%;
  background-color: rgba(0, 150, 136, 0.78);
}
.nav-center {
  max-width: 1100px;
  min-height: 50px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
span {
  color: white;
  font-size: 14px;
  font-weight: 600;
}
.nav-email {
  max-width: 11em;
  text-align: center;
  display: flex;
  // padding-top: 10px;
  padding-left: 4px;
  box-sizing: border-box;
}
.nav-email > span {
  height: 30px;
  line-height: 30px;
}
.nav-email i {
  width: 30px;
  height: 30px;
  margin-right: 5px;
}
.nav-email i img {
  width: 30px;
  height: 30px;
}
.nav-phone {
  height: 100%;
  max-width: 11em;
  text-align: center;
  // padding-top: 10px;
  display: flex;
  padding-left: 4px;
  box-sizing: border-box;
  // background-color: red;
}
.nav-phone > span {
  text-align: left;
  height: 30px;
  line-height: 30px;
}
@media only screen and (min-width: 200px) and (max-width: 838px) {
  .nav-email span {
    line-height: 30px;
    font-size: 12px;
  }
  .nav-phone {
    width: 15em;
  }
  .nav-phone span {
    font-size: 12px;
  }
  .ul-div {
    margin-left: 20px;
    // background-color: yellow;
  }
}
.nav-phone i {
  width: 30px;
  height: 30px;
  margin-right: 5px;
}
.nav-phone i img {
  width: 30px;
  height: 30px;
}
.nav-ul {
  float: right;
  display: flex;
  list-style: none;
  margin: auto 0;
  height: 50px;
  width: 150px;
  line-height: 50px;
  box-sizing: border-box;
  // background-color: yellow;
}
.nav-ul li {
  box-sizing: border-box;
  margin-right: 0.3rem;
  width: 30px;
  // height: 30px;
  box-sizing: border-box;
  padding-top: 8px;
  border-radius: 4px;
  // background-color: red;
}
.nav-ul img {
  width: 23px;
  height: 23px;
}
a {
  text-decoration: none;
  color: #000;
  width: 23px;
  height: 23px;
  margin: auto;
  display: block;
}
.nav-ul li:nth-child(1):hover {
  width: 40px;
  // height: 40px;
  transform: scale(1);
  transition: 0.5s linear;
  box-shadow: 0 0 20px rgba(59, 88, 152, 0.9);
  background-color: rgba(46, 64, 100, 0.87);
}
.nav-ul li:nth-child(2):hover {
  width: 40px;
  // height: 40px;
  transform: scale(1);
  transition: 0.5s linear;
  box-shadow: 0 0 20px rgba(39, 168, 223, 0.78);
  background-color: rgba(93, 206, 255, 0.87);
}
.nav-ul li:nth-child(3):hover {
  width: 40px;
  // height: 40px;
  transform: scale(1);
  transition: 0.5s linear;
  opacity: 0.87;
  box-shadow: 0 0 20px pink;
  background: url("http://192.168.2.27:8888/assets/img/pinkback.jpg") no-repeat
    center center;
}
.nav-ul li:nth-child(4):hover {
  width: 40px;
  transform: scale(1);
  transition: 0.5s linear;
  box-shadow: 0 0 20px rgba(144, 238, 144, 0.78);
  background-color: rgba(144, 238, 144, 0.87);
}
.ul-div {
  // width: 40%;
  position: relative;
}

.user-info-center {
  // background-color: red;
  display: block;
  margin-right: 2%;
  height: 30px;
  line-height: 30px;
  box-sizing: border-box;
}
.user-info-center:hover {
  border-bottom: 2px solid #fff;
}
.user-info-span {
  margin: 0 auto;
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.exit-user-btn {
  border: none;
  margin: auto;
  font-size: 12px;
  padding: 0 2%;
  background-color: rgb(11, 80, 73);
}
.top-right-media {
  width: 30px;
  height: 30px;
  float: right;
  margin-right: 20px;
  background: url("http://192.168.2.27:8888/assets/img/choose.png") no-repeat 0
    0;
  transform-origin: center;
  transition: 0.3s linear;
  background-size: cover;
  display: none;
}
.user-info-div {
  display: flex;
  // position: absolute;
  // width: 100%;
  // top: 50%;
  // left: 50%;
  // transform: translate(-50%, -50%);
  box-sizing: border-box;
  justify-content: space-between;
  >span {
    padding: 0 3px;
    white-space: nowrap;
  }
}
@media screen and (max-width: 768px) {
  .user-info-div {
    flex-direction: column;
    padding-bottom: 2%;
  }
  .exit-user-btn {
    width: 60%;
    margin: 30px auto 0 auto;
  }
}
@media screen and (max-width: 935px) {
  .user-info-div {
    display: none;
  }
  .top-right-media {
    display: block;
  }
  .user-info-center:hover {
    color: #2cf1e8;
    border-bottom: none;
  }
}
// .ul-div {
//   // background-color: red;
//   position: relative;
// }
.user-info-div-media {
  background-color: rgba(0, 121, 109, 0.78);
  z-index: 200;
  position: absolute;
  top: 105%;
  right: 3%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  transform: scaleY(1);
  transform-origin: center top;
  opacity: 1;
  transition: 0.3s linear;
}
.openlistdiv {
  transform: scaleY(0);
  opacity: 0;
}
.openspan {
  transform: rotate(90deg);
}
.closespan {
  transform: rotate(0deg);
}
.exit {
  display: none;
}
@media screen and (min-width: 936px) {
  .user-info-div-media {
    display: none;
  }
}
</style>